import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {AppUrl} from "../../../app/app.general";
import {HttpClient} from "@angular/common/http";

/**
 * Generated class for the BusinessFoodMinusPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-business-food-minus',
  templateUrl: 'business-food-minus.html',
})
export class BusinessFoodMinusPage {

  private tableName: string;
  private sumCount: any;
  realGet: any;
  bal: any;
  private orderDetail: any;
  listDetail:any= [];

  private floor: number;
  orderno:string;
  callback:any;
  tableid:number;
  pagetitle:string;

  constructor(public navCtrl: NavController, public navParam: NavParams, private http: HttpClient, private toastCtrl: ToastController) {
    this.floor = this.navParam.get('floor');
    this.tableName = this.navParam.get('data').tablename;
    this.orderno=this.navParam.get('data').orderno;
    this.tableid=this.navParam.get('data').tableid;
    this.pagetitle=this.navParam.get('pagetitle');
    this.orderDetail = this.navParam.get('data');
    if(this.orderno!=null){
      console.log(this.orderno);
      this.http.post(AppUrl.food.cashDetail,{orderno:this.orderno}).subscribe(data=>{
        console.log(data);
        let temp:any=data;
        this.listDetail=temp.data;
      });
    }else{
      console.log(this.tableid);
      this.http.post(AppUrl.food.cashInfo,{tableno:this.tableid}).subscribe(data=>{
        console.log(data);
        let temp:any=data;
        this.listDetail=temp.data;
      });
    }
    this.callback=this.navParam.get('callback');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BusinessFoodMinusPage');
  }

  presentToast(message) {
    let toast = this.toastCtrl.create({
      message: message,
      duration: 3000
    });
    toast.present();
  }
  updateFood(index){
    this.orderno = this.listDetail[index].orderno;
    let foodid:any = this.listDetail[index].foodid;
    let minusPrice:any = this.listDetail[index].price ; //每次只能减1
    let num:any = this.listDetail[index].num;
    if(num <=1 ){
      this.presentToast("不能再减了哦,直接删除吧");
      return;
    }
    this.http.post(AppUrl.food.updOrderFood,{foodId:foodid,orderNo:this.orderno,minusPrice:minusPrice}).subscribe(data=>{
      let temp:any=data;
      if(temp.code == 200){
        this.listDetail[index].num= num - 1; //每次减1
      }
    });
  }

  delFood(index){
     this.orderno = this.listDetail[index].orderno;
     let foodid:any = this.listDetail[index].foodid;
     let minusPrice:any = this.listDetail[index].price * this.listDetail[index].num ;
    this.http.post(AppUrl.food.delOrderFood,{foodId:foodid,orderNo:this.orderno,minusPrice:minusPrice}).subscribe(data=>{

      let temp:any=data;
      if(temp.code == 200){
        this.listDetail.splice(index, 1);
      }

    });
  }



}
